import java.util.Arrays;

public class Solution {

        public static int[] sortedSquares(int[] nums) {
            int len = nums.length;
            int left = 0;
            int right = len - 1;
            int[] ret = new int[len];
            while (left <= right) {
                if (nums[right] * nums[right] > nums[left] * nums[left]) {
                    ret[len - 1] = nums[right] * nums[right];
                    len--;
                    right--;
                } else {
                    ret[len - 1] = nums[left] * nums[left];
                    len--;
                    left++;
                }
            }
            return ret;
        }
    public static void main(String[] args) {
       int[] nums = {-4,-1,0,3,10};
       int[] ret = sortedSquares(nums);
        System.out.println(Arrays.toString(ret));

    }
}